package cn.kgc.service;

import cn.kgc.common.CrowdFundingUtils;
import cn.kgc.mapper.UserMapper;
import cn.kgc.pojo.User;
import cn.kgc.pojo.UserExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-03-08 14:45
 */
@Service("userService")

public class UserServiceImpl implements  UserService {


    @Resource
    UserMapper userMapper;

    //分页 根据姓名查询
    @Override
    public PageInfo<User> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);

        if (CrowdFundingUtils.stringEffective(keyword)) {
            UserExample example=new UserExample();
            UserExample.Criteria criteria = example.createCriteria();
            //根据姓名查询

            criteria.andNameLike("%"+keyword+"%");
            UserExample.Criteria criteria1 = example.createCriteria();

            List<User> users = userMapper.selectByExample(example);
            PageInfo<User> pageInfo=new PageInfo<>(users);
            return pageInfo;
        }else{
            List<User> users = userMapper.selectByExample(null);
            PageInfo<User> pageInfo=new PageInfo<>(users);
            return pageInfo;
        }
    }

    //详细信息
    @Override
    public User selectByIdUser(Integer id) {

        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> getUserListByIdList(List<Integer> userIdList) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(userIdList);

        return userMapper.selectByExample(example);
    }

    @Override
    public void batchRemove(List<Integer> userIdList) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(userIdList);
        userMapper.deleteByExample(example);
    }

    @Override
    public void remove(Integer id) {

       userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public User selectById(Integer id) {

        return    userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateUser(User user) {
        String userPswd=user.getUserpassword();
        userPswd= CrowdFundingUtils.md5(userPswd);

        user.setUserpassword(userPswd);
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void saveUser(User user) {
        String userPswd=user.getUserpassword();
        userPswd= CrowdFundingUtils.md5(userPswd);
        user.setUserpassword(userPswd);
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {
        String userPswd=user.getUserpassword();
        userPswd= CrowdFundingUtils.md5(userPswd);
        user.setUserpassword(userPswd);
        userMapper.updateByPrimaryKeySelective(user);
    }


//    @Override
//    public void addUser(User user) {
//         userMapper.insert(user);
//    }
}
